package EnrollmentController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.renjun.smweb.commons.util.Encrypt;
import com.renjun.smweb.dao.pojo.LoginInfo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.renjun.smweb.controller.EnrollmentController.EnrollmentController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by huamin on 2017/2/23.
 * 招生设置M5
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath*:/spring-mvc.xml", "classpath*:/spring-mybatis.xml"})
public class EnrollmentControllerTestCase {

    @Autowired
    private EnrollmentController enrollmentController;

    //模拟request,response
    private MockHttpServletRequest request;
    private MockHttpServletResponse response;

    @Before
    public void setUp() throws Exception {
        request = new MockHttpServletRequest();
        request.getSession().setAttribute("userinfo", getLoginInfo());
//        response = new MockHttpServletResponse();
    }

    private LoginInfo getLoginInfo() {
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setId("b9a1c118-d92a-4190-8177-f99e58221fb2");
        loginInfo.setRoleid("2");
        loginInfo.setDistrictcode("310116");
        loginInfo.setRoletype("2");
        loginInfo.setUsername("测试123");
        loginInfo.setRealname("junit测试管理员");
        return loginInfo;
    }

    /**
     * 招生计划列表 M5-1
     * @param
     * @return
     */
    @Test
    public void testEnrollPlanList(){
        System.out.println("M5-1招生计划列表");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        params.put("limit","10");
        params.put("offset","0");
        params.put("order","");
        params.put("sort","");
        params.put("districtcode","310116");
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //M5-1招生计划列表
        JSONObject rs = enrollmentController.enrollPlanList(request, response);
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" + Encrypt.jiem(jiem)+ "\n");
    }

    /**
     * 招生计划列表统计 M5-9
     * @param
     * @return
     */
    @Test
    public void testEnrollPlanListStat(){
        System.out.println("M5-9招生计划列表统计");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        params.put("limit","10");
        params.put("offset","0");
        params.put("order","");
        params.put("sort","");
        params.put("districtcode","310116");
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //M5-9招生计划列表统计
        JSONObject rs = enrollmentController.enrollPlanListStat(request, response);
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" + Encrypt.jiem(jiem)+ "\n");
    }

    /**
     * 招生计划列表导出 M5-3
     * @param
     * @return
     */
    @Test
    public void testExpEnrollPlanList(){
        System.out.println("M5-3招生计划列表导出");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        params.put("limit","10");
        params.put("districtcode","310116");
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //M5-3招生计划列表导出
        JSONObject rs = enrollmentController.expEnrollPlanList(request, response);
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" + Encrypt.jiem(jiem)+ "\n");
    }

    /**
     * 单个修改招生计划 M5-5
     * @param
     * @return
     */
    @Test
    public void testModifyEnrollPlan(){
        System.out.println("M5-5单个修改招生计划");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        validateModifyEnrollPlan(params);
   //     validateEmptyEnrollPlan(params);
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //M5-5单个修改招生计划
        JSONObject rs = enrollmentController.modifyEnrollPlan(request, response);
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" + Encrypt.jiem(jiem)+ "\n");
    }

    //校验正确单个修改招生计划
    private void validateModifyEnrollPlan(Map<String, Object> params){
        params.put("tbbs","10");
        params.put("tbrs","10");
        params.put("xbbs","10");
        params.put("xbrs","10");
        params.put("id","5a36fbfd-a1e8-42e1-91fc-2ea2c30406ad");
        params.put("adminid","b9a1c118-d92a-4190-8177-f99e58221fb2");
    }

    //校验某个值为空
    private void validateEmptyEnrollPlan(Map<String, Object> params){
        params.put("tbbs","");
        params.put("tbrs","");
        params.put("xbbs","");
        params.put("xbrs","");
        params.put("id","5a36fbfd-a1e8-42e1-91fc-2ea2c30406ad");
        params.put("adminid","b9a1c118-d92a-4190-8177-f99e58221fb2");
    }

    /**
     * 录取信息下载设置 M5-6
     * @param
     * @return
     */
    @Test
    public void testUpdateEnrollOpenTime(){
        System.out.println("M5-6录取信息下载设置");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        validateUpdateEnrollOpenTime(params);
   //     validateWrongUpdateEnrollOpenTime(params);
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //M5-6录取信息下载设置
        JSONObject rs = enrollmentController.updateEnrollOpenTime(request, response);
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" + Encrypt.jiem(jiem)+ "\n");
    }

    //校验正确修改时间
    private void validateUpdateEnrollOpenTime(Map<String, Object> params){
        params.put("opentype","1");
        params.put("starttime","20170315");
        params.put("endtime","20170526");
        params.put("schoolgrade","1");
    }

    //校验错误修改时间
    private void validateWrongUpdateEnrollOpenTime(Map<String, Object> params){
        params.put("opentype","1");
        params.put("starttime","20170615");
        params.put("endtime","20170526");
        params.put("schoolgrade","1");
    }

    /**
     * 录取设置预加载 M5-7
     * @param
     * @return
     */
    @Test
    public void testQueryEnrollOpenTime(){
        System.out.println("M5-7录取设置预加载");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        params.put("opentype","1");
        params.put("districtcode","310116");
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //M5-7录取设置预加载
        JSONObject rs = enrollmentController.queryEnrollOpenTime(request, response);
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" + Encrypt.jiem(jiem)+ "\n");
    }
}
